<?php

function append_title($title , $color  = true){
    $old_title = sfContext::getInstance()->getResponse()->getTitle();
    sfContext::getInstance()->getResponse()->setTitle( $old_title.' - '.$title );
    $title_slot = $color ? '<span>'.implode('</span> <span>',explode(' ',$title)).'</span>' : '<span></span><span>'.$title.'</span>';
    sfContext::getInstance()->getResponse()->setSlot('title_slot' ,  $title_slot);
}


function link_to_language_chnger($languges){

    $current_link = $_SERVER['REQUEST_URI'];
    if(count($languges) == 2){//en , ar
        $current_lang = sfContext::getInstance()->getUser()->getCulture();

        $label     = $languges[$current_lang];
        unset ( $languges[$current_lang] );
        $new_lang  = key($languges);


        /*        $new_link = str_replace( '/'.$current_lang ,'/'.$new_lang, $current_link);

        $new_link = strpos($new_link,'/'.$new_lang)===false  ? $new_link.'/'.$new_lang : $new_link ;
        */

        $new_link = url_for('@homepage_culture?sf_culture='.$new_lang);


        return link_to( $label , $new_link);
    }
}


function include_menu($menu_name , $options = array() ){

    //if($menu_name instanceof Menu ){
    if(is_object($menu_name)){

        $menu = $menu_name;

    }else{
        $menu = MenuPeer::retrieveByName( $menu_name );
    }


    if(!$menu){
        throw new Exception(sprintf( 'Menu object of name ( %s ) is not exsited' , $menu_name ));
    }


    $menu_items = $menu->getRootMenuItems( 0 );

    echo _draw_menu( $menu_items , 1 , $options );


}




function _draw_menu($menu_items , $level = 1, $options = array() ){
    /*@var $menu_item MenuItem*/
    $html = "";
    $sf_user = sfContext::getInstance()->getUser();
    foreach ($menu_items as $menu_item){
        // count of childrens
        $children_count = $menu_item->countMenuItemsRelatedByParentId()	;
        if( $children_count > 0 ){
            if(get_menu_option('links_only' , $level , $options )){
                $html .= link_to_menu($menu_item)._draw_menu( $menu_item->getMenuItemsRelatedByParentId() , $level+1 , $options );
            }else{
                $html .=
                "\n<li class='".get_menu_option('extra_li_class' , $level , $options )."'>"
                .link_to_menu($menu_item)._draw_menu( $menu_item->getMenuItemsRelatedByParentId() , $level+1 , $options )
                ."</li>";

            }

        } else {




            $link_perms = $menu_item->getMenuItemPermissionsJoinsfGuardPermission();

            // menu item default state can go unless there is permission assosiated with
            $can_go = true;

            // if menu item has permissions links then go on checking permisson list
            if( count($link_perms) > 0 ){
                // cahnge the default sate to false until checking user has the one of the right permes
                $can_go = false;

                // no need to continue if the user is not logged in
                if(!$sf_user->isAuthenticated()){
                    continue ;
                }

                foreach ($link_perms as $link){
                    $perm_name = $link->getsfGuardPermission()->getName();

                    if($sf_user->hasCredential($perm_name)){
                        $can_go = true;
                        break;
                    }
                }

            }// count perms

            if($can_go){
                //$items_arr[] =  link_to($menu_item->getTitle(),$menu_item->getAliasUrl()

                if ( get_menu_option('links_only' , $level , $options ) ){
                    $html .= sprintf ( get_menu_option ('a_wrapper' , $level , $options ) , link_to_menu( $menu_item ) );
                }else{
                    $html .="\n<li class='level-{$level} ".get_menu_option( 'extra_li_class' , $level , $options )."'>". sprintf(get_menu_option('a_wrapper' , $level , $options ) , link_to_menu($menu_item , $level , $options) ).'</li>';
                }
            }

        }

    }
    if(get_menu_option('links_only' , $level , $options )){
        return $html;
    }else{
        return "<ul class='level-{$level} ".get_menu_option('extra_ul_class' , $level , $options )."'>\n{$html}\n</ul>";
    }


}


function link_to_menu( $menu_item , $level = 1 , $options = array() ){
    if($menu_item->getIsSeparator()){
        return '<a href="javascript:;" class="separator">'.$menu_item.'</a>';
    }

    $title =  sprintf( get_menu_option('text_wrapper' , $level , $options ) , $menu_item->getTitle() );
    
    return link_to($title,url_for_menu($menu_item)
    ,array(
    'absolute' => $menu_item->getIsAbsolute()
    , 'target'   => $menu_item->getIsTargetBlank() ? '_blank' : null
    ));
}

function url_for_menu($menu_item){
    //if(!$menu_item instanceof MenuItem ){
    if(!is_object($menu_item) ){
        $menu_item = MenuItemPeer::retrieveByAliasUrl($menu_item);
    }
    if(!$menu_item){
        throw new Exception(sprintf('parameter should be instence of MenuItem , %s is given',get_class($menu_item)));
    }
    if($menu_item->getAliasUrl()){
        return url_for2('menu_item2' , $menu_item);
    }elseif($menu_item->getRoute()){
        return url_for($menu_item->getRoute());
    }

    throw new Exception('Menu item alias is null && route is null ,, one of them must be existed');


}


function get_menu_option( $option_name , $level = 1 , array &$options ){
    $defaults = array(
    'extra_ul_class' => ''
    , 'extra_li_class' => ''
    , 'a_wrapper' => '%s'
    , 'text_wrapper' => '%s'
    , 'links_only' => false
    );


    if( isset( $options[ 'level-'.$level ][$option_name] ) ){
        return $options[ 'level-'.$level ][ $option_name ];

    }elseif( isset( $options[ 'level-all' ][ $option_name ]) ){
        return $options[ 'level-all' ][ $option_name ];
    }else{ //defaults
        return $defaults[ $option_name ];
    }

}






